/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.12
import org.ukui.tablet 1.0
import "../baseComponent/"
import "../launcherController.js" as Controller

LauncherGridViewDelegateItem {
    id: itemRoot
    width: modelData.columnSpan * cellSize
    height: modelData.rowSpan * cellSize

    property int groupId: eduRoot.groupId

    states: [
        State {
            when: itemRoot.Drag.active
            ParentChange {
                target: itemRoot
                parent: listView
            }

            AnchorChanges {
                target: itemRoot
                anchors.horizontalCenter: undefined
                anchors.top: undefined
            }
        }
    ]

    MouseArea {
        id: dragMousearea
        anchors.fill: parent
        hoverEnabled: false
        pressAndHoldInterval: modelData.state === LauncherItem.Editable ? 100 : undefined
        property bool isScrolling: false
        drag {
            maximumX: listView.width - itemRoot.width/2 + 10
            minimumX: -itemRoot.width/2 - 10
            maximumY: listView.height - itemRoot.height/2
            minimumY: -itemRoot.height/2
        }

        Timer {
            id: scrollingTimer
            interval: 800
            onTriggered: dragMousearea.isScrolling = false
        }

        onPressAndHold: {
            if (modelData.type === LauncherItem.AddIcon) {
                return;
            }
            startDrag(true)
            modelManager.setItemsInGroupState(LauncherItem.Editable, groupId)
            dragMousearea.Drag.active = true
            dragMousearea.drag.target = itemRoot
        }

        onClicked: {
            if (modelData.type === LauncherItem.AddIcon) {
                showAddAppWindow(eduRoot.groupId, modelData.pageIndex)
            } else if (modelData.type === LauncherItem.AppIcon && modelData.state === LauncherItem.Normal) {
                appManager.runApplication(modelData.desktopFile)
            }
            console.log(pressAndHoldInterval)
        }
        onPositionChanged:  {
            Controller.eduAppItemPositionChanged(dragMousearea,
                                                 item,
                                                 listView,
                                                 scrollingTimer,
                                                 eduRoot.groupId)
        }
        onPressed: {
            if (modelData.type === LauncherItem.AddIcon) {
                iconpressAnimation.target = item
                iconpressAnimation.start()
            }
        }

        onReleased: {
            if (modelData.type === LauncherItem.AddIcon) {
                iconreleseAnimation.target = item
                iconreleseAnimation.start()
                return
            }
            startDrag(false)
            isScrolling = false
            dragMousearea.Drag.active = false
            dragMousearea.drag.target = null
            modelManager.replacePlaceholderItem(itemRoot.visualIndex, itemRoot.pageIndex, groupId)
            modelManager.updateCachedData(groupId)
        }
    }

    EduAppIcon {
        id: item
    }

    GridViewDelegateItemText {
        id: itemName
        width: (item.width + 40) * Style.widthScaleFactor
        anchors { top: item.bottom; topMargin: 12
                  horizontalCenter: item.horizontalCenter }
        visible: modelData.type !== LauncherItem.Placeholder
    }

    ScaleAnimator {
        id: iconpressAnimation
        from: 1
        to: 0.8
        duration: 150
    }
    ScaleAnimator {
        id: iconreleseAnimation
        from: 0.8
        to: 1
        duration: 150
    }
}
